#pragma once

#include "noncopyable.h"
#include "Socket.h"
#include "Channel.h"

class EventLoop;
class InetAddress;

class Acceptor : noncopyable
{
public:
    using NewConnectionCallback = std::function<void(int sockfd, InetAddress &)>;

    Acceptor(EventLoop *loop, const InetAddress &listenAddr, bool resueport);
    ~Acceptor();

    void setNewConnectionCallback(const NewConnectionCallback &cb)
    {
        newConnectionCallback_ = cb;
    }

    bool listenning() const { return listenning_; }
    void listen();

private:
    void handleRead();

    EventLoop *loop_; // 绑定的事件循环，Acceptor用的就是用户定义的那个baseLoop，也称作mainLoop
    Socket acceptSocket_;
    Channel acceptChannel_;                       // 将listenfd的读事件与回调绑定
    NewConnectionCallback newConnectionCallback_; // 新连接的回调函数，由TcpServer设置
    bool listenning_;
};
